using System;
using System.ServiceModel;
using System.Linq;
using System.ServiceModel.Activation;

namespace StructureMap.WcfIntegration
{
    public class StructureMapServiceHostFactory : ServiceHostFactory
    {
        
        public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
        {
            var instance = ObjectFactory.Model.PluginTypes.Where(x => x.Instances.Count(y=>y.Name == constructorString) > 0).FirstOrDefault();


            return CreateServiceHost(instance.PluginType, baseAddresses);
        }

        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
           
            return new StructureMapServiceHost(serviceType,ObjectFactory.GetInstance<IContainer>(),baseAddresses);
        }
    }

} 